var appconfig = require('../../../utils/appconfig');
var Q = require('q');
var util = require('util');


var CFG_FILE_PATH = 'F:/works/nodeapp/ted/cfg.yml';

var mongos = null;
describe('mongodb unit', function () {
  var _cfg = null;
  before(function(){

    var cfgfile = require('fs').readFileSync(CFG_FILE_PATH, 'utf8');
    appconfig.init(cfgfile);    
    _cfg = appconfig.getCfg();

    mongos = require('../../../service/mongodb');    
  });


  xit('first get hot topic ,second get the topic detail', function (done) {
    mongos.get('homepageconfig',{})
    .then(function(doc){
      return mongos.get('topics',{'tid':doc.hottopic});
    })
    .then(function(doc){
      expect(doc).not.toBeNull();
      done();
    });
  });

  xit('first find hot topic ,second find the topic detail', function (done) {
    mongos.find('homepageconfig',{})
    .then(function(docs){
      expect(docs).not.toBeNull();
      expect(docs.length).toBeGreaterThan(0);
      return mongos.find('topics',{'tid':docs[0].hottopic},'startdate:desc');
    })
    .then(function(docs){
      expect(docs).not.toBeNull();
      expect(docs.length).toBeGreaterThan(0);
      done();
    });
  });  


  xit('find the topic detail order by startdate:desc', function (done) {
    mongos.find('topics',{},'startdate:desc')
    .then(function(docs){
      expect(docs).not.toBeNull();
      expect(docs.length).toBeGreaterThan(0);
      console.log(docs[0]);
      done();
    });
  }); 

  it('update mycls', function (done) {
    mongos.update('mycls',{val:11111},{val:234234,date:new Date()})
    .then(function(doc){
      expect(doc.val).toBe(11111);
      done();
    }).catch(function(d){
      done();
    });
  });   
});